package com.granada.model.common;

import com.granada.common.Curd;
import com.granada.common.CurdExtra;
import com.granada.entity.Ao;
import com.granada.entity.Page;
import com.granada.entity.SysRolePermission;
import com.granada.model.common.base.RolePermissionBase;
import com.granada.vo.req.RolePermissionOperationReqVO;
import com.granada.vo.resp.PermissionRespNode;

import java.util.List;
import java.util.Set;

public abstract  class RolePermission<T extends SysRolePermission,R extends RolePermissionOperationReqVO,F extends Page> extends RolePermissionBase<T,R,F> implements Curd<T>, CurdExtra<T> {
    public T vo;
    public R ro;
    public F fo;
    public Ao ao;

    public RolePermission(T vo, R ro,F fo, Ao ao) {
        this.vo = vo;
        this.ro = ro;
        this.fo = fo;
        this.ao = ao;
    }
    public Long add() throws Exception {
        ao.id = this.addRolePermissionBefore();
        this.addRolePermissionAfter();
        return null;
    }
    public void addBatch() throws Exception{
        this.addBatchRolePermissionBefore();
        this.addBatchRolePermissionAfter();

    }

    public  void update() throws Exception {
        this.updateRolePermissionBefore();
        this.updateRolePermissionAfter();
    }

    public void delete() throws Exception {
        this.deleteRolePermissionBefore();
        this.deleteRolePermissionAfter();
    }

    public void batchDelete() throws Exception {
    }


    public abstract Long addRolePermissionBefore() throws Exception;
    public abstract void addRolePermissionAfter() throws Exception;
    public abstract void addBatchRolePermissionBefore() throws Exception;
    public abstract void addBatchRolePermissionAfter() throws Exception;

    public abstract void updateRolePermissionBefore() throws Exception;
    public abstract void updateRolePermissionAfter() throws Exception;

    public abstract void deleteRolePermissionBefore() throws Exception;
    public abstract void deleteRolePermissionAfter() throws Exception;
    private void setheckced(List<PermissionRespNode> list, Set<String> checkList){
        for(PermissionRespNode node:list){
            if(checkList.contains(node.getId()) && (node.getChildren()==null || node.getChildren().isEmpty())){
                node.setChecked(true);
            }
            setheckced((List<PermissionRespNode>) node.getChildren(),checkList);
        }
    }
}
